import { fileURLToPath, URL } from 'node:url'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  // 可选：加载环境变量（通常不需要，Vite 自动处理）
  // const env = loadEnv(mode, process.cwd())

  return {
    plugins: [vue()],
    base: './',
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `
            @use "@/styles/variables.scss" as *;
            @use "@/styles/mixins.scss" as *;
            @use "@/styles/transitions.scss" as *;
          `
        }
      }
    },
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    server: {
      host: '127.0.0.1',
      port: 3000,
      proxy: {
        '/api': {
          target: 'http://43.138.15.137:3000/api/',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    },
    build: {
      // 使用 terser 进行压缩（支持 drop_console）
      minify: 'terser',
      terserOptions: {
        compress: {
          // 仅在生产构建时移除 console
          drop_console: mode === 'production',
          drop_debugger: mode === 'production'
        }
      }
    }
  }
})